{{ $full_version := .Page.Params.release }}

{{ $parts := split $full_version "." }}
{{ $version := printf "%s.%s" (index $parts 0) (index $parts 1) }}
{{ $patch := int (index $parts 2) }}
{{ $release_name := strings.TrimSuffix ".0" $full_version }}

{{ $page_version := int (index $parts 0) }}
{{ $page_revision := int (index $parts 1) }}
{{ $page_patch := int (index $parts 2) }}

{{ $site_parts := split .Site.Data.args.full_version "." }}
{{ $site_version := int (index $site_parts 0) }}
{{ $site_revision := int (index $site_parts 1) }}
{{ $site_patch := int (index $site_parts 2) }}

{{ $site_normalized_version := add (add (mul $site_version 1000000) (mul $site_revision 1000)) $site_patch }}
{{ $page_normalized_version := add (add (mul $page_version 1000000) (mul $page_revision 1000)) $page_patch }}

{{ if gt $page_normalized_version $site_normalized_version }}
    {{ errorf "Can't have a release note with a greater version then the site's version (%s vs %s)" $full_version $.Site.Data.args.full_version }}
{{ end }}

{{ $home := .Site.GetPage "home" }}
{{ $lang := $home.Lang }}
{{ if eq $lang "en" }}
    {{ $lang = "" }}
{{ else }}
    {{ $lang = "/zh" }}
{{ end }}

{{/* establish latest corresponding patch release */}}
{{ $latest_patch := 0 }}
{{ $latest_patch_url := "" }}

{{ range $page := .Site.RegularPages }}
    {{ $release := $page.Params.release }}
    {{ if $release }}
        {{ $parts := split $release "." }}
        {{ $v := printf "%s.%s" (index $parts 0) (index $parts 1) }}
        {{ $p := int (index $parts 2) }}
        {{ if eq $version $v }}
            {{ if gt $p $latest_patch }}
                {{ $latest_patch = $p }}
                {{ $latest_patch_url = $page.Permalink }}
            {{ end }}
        {{ end }}
    {{ end }}
{{ end }}

{{ $latest_full_version := printf "%s.%d" $version $latest_patch }}

{{ $release_location := "archive" }}
{{ if and .Site.Data.args.preliminary (eq $version .Site.Data.args.version) }}
    {{ $release_location = "preliminary" }}
{{ else if eq $version .Site.Data.versions.main }}
    {{ $release_location = "main" }}
{{ end }}

<div class="relnote-actions call-to-action">
    {{ $change_notes := .Page.GetPage "./change-notes" }}
    {{ if $change_notes }}
        <a class="entry" href="./change-notes">
            <h5>{{ i18n "relnote_change_notes_title" }}</h5>
            <p>{{ i18n "relnote_change_notes_detail" }}</p>
        </a>
    {{ end }}

    {{ $upgrade_notes := .Page.GetPage "./upgrade-notes" }}
    {{ if not $upgrade_notes }}
        {{ $base_version := printf "%v.%v.0" $page_version $page_revision }}
        {{ range $page := .Site.Pages }}
            {{ $release := $page.Params.release }}
            {{ if eq $release $base_version }}
                {{ $path := printf "/%supgrade-notes" $page.File.Dir }}
                {{ $upgrade_notes = .Page.GetPage $path }}
            {{ end }}
        {{ end }}
    {{ end }}

    {{ if $upgrade_notes }}
        <a class="entry" href="{{ $upgrade_notes.Permalink }}">
            <h5>{{ i18n "relnote_before_you_upgrade_title" }}</h5>
            <p>{{ i18n "relnote_before_you_upgrade_detail" }}</p>
        </a>
    {{ end }}

    {{ if eq $release_location "preliminary" }}
        <div class="entry">
            <h5>{{ i18n "relnote_download_title" }}</h5>
            <p>{{ i18n "relnote_download_detail" }}</p>
        </div>
    {{ else }}
        {{ if ne $latest_full_version $full_version }}
            {{ .Page.Scratch.Set "needPopper" true }}
            <a class="update-notice entry"
                data-title='{{ i18n "relnote_update_notice" }}'
                data-downloadhref="https://github.com/istio/istio/releases/tag/{{ $full_version }}"
                data-downloadbuttontext="DOWNLOAD {{ $full_version }}"
                data-updateadvice='{{ printf (i18n "relnote_update_advice") $release_name }}'
                data-updatebutton='{{ printf (i18n "relnote_update_button") $latest_full_version }}'
                data-updatehref="{{ $latest_patch_url }}">
                <h5>{{ i18n "relnote_download_title" }}</h5>
                <p>{{ i18n "relnote_download_detail" }}</p>
            </a>
        {{ else }}
            <a class="entry" href="https://github.com/istio/istio/releases/tag/{{ $full_version }}">
                <h5>{{ i18n "relnote_download_title" }}</h5>
                <p>{{ i18n "relnote_download_detail" }}</p>
            </a>
        {{ end }}
    {{ end }}

    {{ $doc_link := printf "https://istio.io%s/docs" $lang }}
    {{ if eq $release_location "archive" }}
    {{ else if eq $release_location "preliminary" }}
        {{ $doc_link = printf "https://preliminry.istio.io%s/docs" $lang }}
    {{ else if .Site.Data.args.preliminary }}
        {{ $doc_link = "https://istio.io/docs" }}
    {{ end }}

    <a class="entry" href="{{ $doc_link }}">
        <h5>{{ i18n "relnote_docs_title" }}</h5>
        <p>{{ i18n "relnote_docs_detail" }}</p>
    </a>

    {{ $helm_changes := .Page.GetPage "./helm-changes" }}
    {{ if $helm_changes }}
        <a class="entry" href="./helm-changes">
            <h5>{{ i18n "relnote_helm_changes_title" }}</h5>
            <p>{{ i18n "relnote_helm_changes_detail" }}</p>
        </a>
    {{ end }}

    {{ if $patch }}
        {{ $old_full_version := printf "%v.%d" $version (sub $patch 1) }}
        {{ $delta_link := printf "https://github.com/istio/istio/compare/%s...%s" $old_full_version $full_version }}

        <a class="entry" href="{{ $delta_link }}">
            <h5>{{ i18n "relnote_source_changes_title" }}</h5>
            <p>{{ i18n "relnote_source_changes_detail" }}</p>
        </a>
    {{ end }}
</div>
